package com.xj.center.data.api;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ie.api.center.data.fegin.ControlInfoClient;
import com.xj.center.data.service.ControlInfoService;
import com.xj.center.data.service.PointValueService;
import com.xj.common.bean.Pages;
import com.xj.common.bean.R;
import com.xj.common.bean.constant.Common;
import com.xj.common.bean.em.ControlStateEm;
import com.xj.common.bean.point.PointValue;
import com.xj.common.dto.ControlInfoDto;
import com.xj.common.dto.PointValueDto;
import com.xj.common.model.ControlInfo;
import com.xj.common.utils.JsonPaseUtil;
import com.xj.common.utils.KafkaProducer;
import com.xj.common.utils.RedisUtil;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 设备控制信息接口
 * @author wsp
 *
 */
@RestController
@RequestMapping(value = Common.Service.IE_CTRL)
public class ControlInfoAPI implements ControlInfoClient {
    @Resource
    private ControlInfoService controlInfoService;


    @RequestMapping(value = Common.Service.IE_CTRL_SEND,method = RequestMethod.POST,produces = "application/json;charset=UTF-8")
    @CrossOrigin
    @Override
    public R<String> sendCtl(JSONObject jsonParam) {
        String deviceSN = jsonParam.getString("sn");
        String clientID = jsonParam.getString("clientid");
        String parameter = jsonParam.getJSONObject("parameters").toJSONString();
        //组装ControlInfo,发送到驱动层进行执行
        ControlInfo controlInfo = new ControlInfo(deviceSN,clientID, ControlStateEm.CTL_STATE_RECEIVED.getCode(), parameter);
        String handle = controlInfoService.sendCtl(controlInfo);
        if(handle==null)
            return R.fail("下控令错误！");
        return R.ok(controlInfo.getId());
    }

    @RequestMapping(value = Common.Service.IE_CTRL_GETRESULT,method = RequestMethod.POST,produces = "application/json;charset=UTF-8")
    @CrossOrigin
    @Override
    public R<ControlInfo> handleGetCtlResult(JSONObject jsonObject) {

        String handle = jsonObject.getString("handle");
        try{
            ControlInfo controlInfo = controlInfoService.realtimeByCtlId(handle);
            return R.ok(controlInfo);
        }catch (Exception e){
            return R.fail(e.getMessage());
        }
    }
    @RequestMapping(value = Common.Service.IE_CTRL_LIST,method = RequestMethod.POST,produces = "application/json;charset=UTF-8")
    @CrossOrigin
    @Override
    public R<Page<ControlInfo>> handleGetCtllist(JSONObject jsonObject) {
        ControlInfoDto ControlInfoDto = JsonPaseUtil.JSONStrToObj(jsonObject.toJSONString(), ControlInfoDto.class);
        Long deviceId = jsonObject.getLong("deviceId");
        try{
            Page<ControlInfo> controlInfol = controlInfoService.list(ControlInfoDto);
            return R.ok(controlInfol);
        }catch (Exception e){
            return R.fail(e.getMessage());
        }

    }

    @RequestMapping(value = Common.Service.IE_CTRL_GETLATESTCTLINFO,method = RequestMethod.POST,produces = "application/json;charset=UTF-8")
    @CrossOrigin
    @Override
    public R<List<ControlInfo>> handleGetLatestCtllistByDeviceID(JSONObject jsonObject) {
        Long deviceId = jsonObject.getLong("deviceId");
        try{
            List<ControlInfo> controlInfol = controlInfoService.latest(deviceId);
            return R.ok(controlInfol);
        }catch (Exception e){
            return R.fail(e.getMessage());
        }
    }
}
